﻿/*
#**************************************************************************
#  openSIS is a student information system for public and non-public 
#  schools from Open Solutions for Education, Inc. web: www.os4ed.com
#
#  openSIS is  web-based, open source, and comes packed with features that 
#  include student demographic info, scheduling, grade book, attendance, 
#  report cards, eligibility, transcripts, parent portal, 
#  student portal and more.   
#
#  Visit the openSIS web site at http://www.opensis.com to learn more.
#  If you have question regarding this system or the license, please send 
#  an email to info@os4ed.com.
#
#  This program is released under the terms of the GNU General Public License  
#  as  published by the Free Software Foundation, version 2 of the License. 
#  See license.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#**************************************************************************** 

*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using OpenSIS.OpenSISWCFServiceReference;

namespace OpenSIS
{
    public partial class UserDropDown : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        { 
            if (Page.IsPostBack == false)
            {
                PopulateSchool(ref ddlSchoolName);               
                PopulateFullYear(ref ddlFullYear);
               // PopulateLevel1(ref ddlLevel1);
            }         
        }

        public void PopulateSchool(ref System.Web.UI.WebControls.DropDownList ddlSchoolName)
        {
            DataSet ds = new DataSet();
            ServicePropertiesClient SPClient = new ServicePropertiesClient();
            ds = SPClient.PopulateSchool();
            SPClient.Close();
            ddlSchoolName.Items.Clear();
            ddlSchoolName.DataSource = ds.Tables[0];
            ddlSchoolName.DataTextField = "sch_full_nm";
            ddlSchoolName.DataValueField = "sch_id";
            ddlSchoolName.DataBind();
            ddlSchoolName.Items.Insert(0, new ListItem("select", "0"));

            if (Session["SchoolID"] != null)
            {
                ddlSchoolName.Items.FindByValue(Session["SchoolID"].ToString()).Selected = true;
            }
        }

        public void PopulateFullYear(ref System.Web.UI.WebControls.DropDownList ddlFullYear)
        {
            DataSet ds = new DataSet();
            ServicePropertiesClient SPClient = new ServicePropertiesClient();

            if (Session["SchoolID"] != null)
            {
                ds = SPClient.PopulateFullYear(Session["SchoolID"].ToString());
            }
            else
            {
                ds = SPClient.PopulateFullYear(ddlSchoolName.SelectedItem.Value);
            }
            SPClient.Close();
            ddlFullYear.Items.Clear();
            ddlFullYear.DataSource = ds.Tables[0];
            ddlFullYear.DataTextField = "schyr_nm";
            ddlFullYear.DataValueField = "schyr_id";
            ddlFullYear.DataBind();
            ddlFullYear.Items.Insert(0, new ListItem("select", "0"));
            ddlFullYear.Items.FindByValue(Convert.ToString(ds.Tables[0].Rows[0][0])).Selected = true;

            PopulateLevel1(ref ddlLevel1);
            if (Session["MP"] != null)
            {
                ddlLevel1.Items.FindByValue(Convert.ToString(Session["MP"])).Selected = true;
            }
            else
            {

                ddlLevel1.Items.FindByValue("0").Selected = true;
            }

            //if (dsSemester.Tables[0].Rows.Count > 0)
            //{
            //    ddl1.Items.FindByValue(Convert.ToString(dsSemester.Tables[0].Rows[0][0])).Selected = true;
            //}

        }

        public void PopulateLevel1(ref System.Web.UI.WebControls.DropDownList ddl)
        {   
            ddl.Items.Clear();
            DataSet ds = null;
            ServicePropertiesClient SPClient = new ServicePropertiesClient();
            ds = SPClient.PopulateLevel1(ddlSchoolName.SelectedItem.Value,ddlFullYear.SelectedItem.Value);
            SPClient.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {

                Session["MP"] = ds.Tables[0].Rows[0][0];
                ddl.DataSource = ds.Tables[0];
                ddl.DataTextField = "sch_mp_name";
                ddl.DataValueField = "schyr_id";
                ddl.DataBind();
            }
                ddl.Items.Insert(0, new ListItem("select", "0"));
        }

        protected void ddlSchoolName_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["SchoolID"] = ddlSchoolName.SelectedItem.Value;
            PopulateFullYear(ref ddlFullYear);
            Response.Redirect(this.Request.Url.ToString());
        }

        protected void ddlFullYear_SelectedIndexChanged(object sender, EventArgs e)
        {
           // Session["MP"] = ddlFullYear.SelectedItem.Value;
        }
    }
}